package com.hbxy.favorite.controller;


import com.hbxy.favorite.dao.CollectMapper;
import com.hbxy.favorite.domain.Collect;


import com.hbxy.favorite.resault.ResponseBo;
import com.hbxy.favorite.service.CollectService;
import com.hbxy.favorite.utils.RegUtil;
import com.hbxy.favorite.vo.CollectVo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;


@Controller
public class CollectController {

    @Autowired
    private  CollectMapper mapper;

    @Autowired
    private CollectService collectService;

    /**
     * 点击按钮跳转收藏页面
     * @param url
     * @param title
     * @param modelMap
     * @return
     */
    @RequestMapping("/collect-page")
    public String collectPage(String url , String title , ModelMap modelMap)  {
         Logger log = LoggerFactory.getLogger(this.getClass());
        String description ="";
        Document doc = null;
        String logoUrl ;
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Element element =doc.select("img").first();
        if(element != null)
        {
            logoUrl =element.attr("src");
        }
        else
            logoUrl ="/ico/favicon.ico";

        if(RegUtil.isNull(logoUrl))
            logoUrl ="/ico/favicon.ico";

        log.info("logo链接路径："+element);
        String text = doc.body().text();
        if(text.length()>=300)
        text =text.substring(0,300);

        modelMap.addAttribute("url",url);
        modelMap.addAttribute("title",title);
        modelMap.addAttribute("description",text);
        modelMap.addAttribute("logoUrl",logoUrl);
        return "collect";
    }

    /**
     *添加收藏的文章
     */
    @RequestMapping("/collect")
    @ResponseBody
    public ResponseBo addCollect(@RequestBody Collect collect){
        ResponseBo responseBo = collectService.addCollect(collect);
        return  responseBo;
    }

    /**
     * 获取收藏的文章
     * @return
     */
    @RequestMapping("/getAllCollect")
    public String getAllCollect(String title ,Model model){

        List<CollectVo> collectList = collectService.getAllCollect(title);

        model.addAttribute("collectList",collectList);
        return "findcollect::collectBox";
    }
    /**
     * 根据收藏夹id获取收藏文章
     * @param id
     * @return
     */
    @RequestMapping("/getCollectByFavoriteId")
    public String getCollectByFavoriteId(Long id , Model model){
        List<CollectVo> collectList = collectService.getCollectByFavoriteId(id);

        model.addAttribute("collectList",collectList);
        return "f-collectByFavorite::collectBox1";
    }

    private Logger log = LoggerFactory.getLogger(this.getClass());


    /**
     * 根据用户id查询收藏
     * @param userId
     * @param model
     * @return
     */
    @RequestMapping("/getCollectByUserId")
    public String getCollectByUserId(Long userId , Model model){
        List<CollectVo> collectList = collectService.getCollectByUserId(userId);
        if(collectList.size()==0){
            CollectVo collcetVo =new CollectVo();
            collcetVo.setLogoUrl("#");
            collcetVo.setCategory("ta还未收藏内容！");
            collcetVo.setUrl("#");
            collectList.add(collcetVo);
        }
        model.addAttribute("collectList",collectList);

        String page="get-collectby-user::getcollectbyuser";
        return page;
    }



    /**
     * 删除收藏的文章（假删）
     */
    @RequestMapping("/deleteCollect")
    @ResponseBody
    public  ResponseBo deleteCollect(@RequestBody Collect collect){

        ResponseBo responseBo =collectService.deleteCollect(collect);
        return  responseBo;
    }

    /**
     * 获取未读列表
     */
    @RequestMapping("/getNoRead")
    public  String getNoReadController(Long userId,Model model){

        List<CollectVo>collectVoList =collectService.getNoRead(userId);

        model.addAttribute("collectList",collectVoList);
        return "include-noRead::collectBox3";
    }

    /**
     * 获取回收站列表
     */
    @RequestMapping("getDeletedList")
    public String getDeleted(Long userid ,Model model){

        List<CollectVo> collectVoList =collectService.getDeleted(userid);

        model.addAttribute("collectList",collectVoList);
        return  "page-delete";
    }

    /**
     * 删除回收站中的内容
     */
    @RequestMapping("Deletedforever")
    @ResponseBody
    public ResponseBo deletedForever(@RequestBody  Collect collect ){

        int flag=collectService.deletedForever(collect);
        if(flag==1)
        return  ResponseBo.ok("删除成功！");
        else
            return  ResponseBo.error("删除失败！");

    }

}
